package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.BaseProduct;
import com.yegs.jltqc.dao.BaseProductRepository;
import com.yegs.jltqc.service.BaseProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseProductServiceImpl implements BaseProductService {

    @Autowired
    private BaseProductRepository baseProductRepository;

    @Override
    public void save(BaseProduct baseProduct) {
        baseProductRepository.save(baseProduct);
    }

    @Override
    public List<BaseProduct> list() {
        return baseProductRepository.findAll();
    }

    @Override
    public BaseProduct get(Long id) {
        return baseProductRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        baseProductRepository.deleteById(id);
    }

    @Override
    public Page<BaseProduct> query(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return baseProductRepository.findAll(request);
        }
        return baseProductRepository.findBaseProductsByName(query.getName(), request);
    }

}


